#include "leaveroomreq.h"

LeaveRoomReq::LeaveRoomReq(const std::string &room)
    : SimpleProtoSrv(PT_LEAVE_ROOM)
{
    this->addString(room);
}

ProtoSrv* LeaveRoomReq::decode(ACE_InputCDR &cdr)
{
    SimpleProtoSrv* request = new LeaveRoomReq();
    if(!SimpleProtoSrv::decode(cdr,request)){
        delete request;
        request = 0;
    }

    return request;
}

ProtoSrv* LeaveRoomReq::execute(ACE_SOCK_Stream &peer)
{
    SimpleProtoSrv *resp = 0;

    std::string room;
    if(this->getString(room,0)){
        resp = new SimpleProtoSrv(PT_RESP_LEAVE_ROOM);

        bool result = RoomManager::getInstance()->removeClientFromRoom(peer,room);
        resp->addLong(result ? E_SUCCESS : E_FAIL);

        if(result){
            std::auto_ptr<SimpleProtoSrv> notify(new SimpleProtoSrv(PT_NOTIFY_LEAVE_ROOM));
            notify->addString(room);
            TetrixServer::getInstance()->broadcast(notify);
        }
    }

    return resp;
}

